/***************************************************************************
 *   Copyleft 2008-2009 by												   *
 *	 Iwanicki Krzysztof													   *
 *   <gamma.mini.pw.edu.pl/~iwanickik>									   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 ***************************************************************************/
#ifndef __VENDETTACONSOLE_H__
#define __VENDETTACONSOLE_H__

#include <vector>
#include <iostream>
#include <sstream>
#include <ostream>
#include <iomanip>
#include <stdio.h>
#include "../../Utils/vString.h"
#include "../../Macros/vMemoryMacros.h"

#define	V_SPACING					30
#define	H_SPACING					10
#define CHAR_SPACING				1
#define LINE_SPACING				14
#define FONT						GLUT_BITMAP_HELVETICA_10
#define FONT_HEADER					GLUT_BITMAP_HELVETICA_18

namespace Vendetta {
	class vConsole {
	protected:
		enum eEntryType {
			ENTRY_NORMAL,
			ENTRY_HEADER
		};
		struct Entry {
			eEntryType Type;
			vString Msg;

			Entry(vString line) {
				Msg = line;
				Type = ENTRY_NORMAL;
			}

			Entry(vString line, vBool bHeader) {
				Msg = line;
				Type = bHeader ? ENTRY_HEADER : ENTRY_NORMAL;
			}
		};

		typedef std::vector<vConsole::Entry>				LineList;
		typedef std::vector<vConsole::Entry>::iterator		LineListIter;

	public:
		static vConsole& Instance();
		virtual ~vConsole();

		vInt AddLine(vBool bHeader, const vChar* print, ...);
		vInt AddLine(const vString& line, vBool bHeader = false);
		vBool EditLine(vInt iIndex, const vChar* print, ...);
		vBool EditLine(vInt iIndex, const vString& line);
		vVoid AddBlankLine();
		
		vVoid PageDown();
		vVoid PageUp();
		
		vVoid SetSize(vInt iWidth, vInt iHeight);
		vVoid Render();

	protected:
		vConsole();

		vVoid prepareToRender();
		vVoid printPage();
		vVoid printLine(vInt iLine);
		vVoid clearBuffer();

	protected:
		static vChar mBuffer [4096];
		LineList mLines;

		vInt mCurrentPage;
		vInt mMaxLinesPerPage;
		vInt mMaxLineLength;
		vInt mWidth;
		vInt mHeight;
	};
}

#endif // __VENDETTACONSOLE_H__